package com.naizhuai.ui.mianbusiness.setting;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.CardView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.naizhuai.R;
import com.naizhuai.network.exception.ApiException;
import com.naizhuai.ui.adapter.ManageDividerItemDecoration;
import com.naizhuai.ui.adapter.business.ThankCardItemAdapterHolder;
import com.naizhuai.ui.adapter.management.TeamMemberListAdapterHolder;
import com.naizhuai.ui.components.EditTextMultiLine;
import com.naizhuai.ui.components.swipeback.SwipeBackActivity;
import com.naizhuai.ui.entity.mainbusiness.ActionSubmitScanDetailsBean;
import com.naizhuai.ui.entity.setting.ThankDetailsData;
import com.naizhuai.ui.user.UserManageDataApiStoreIml;
import com.naizhuai.util.ErrorViewCreateUtils;
import com.naizhuai.util.ImageUtils;
import com.naizhuai.util.IntentUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observer;

/**
 * 感谢卡展示界面
 */
public class ThankCardPagerActivity extends SwipeBackActivity implements View.OnClickListener {
    private static final String TAG = "ThankCardPagerActivity";

    private List<ThankDetailsData> thankDataList;

    @BindView(R.id.error_layout)
    RelativeLayout errorLayout;
    @BindView(R.id.thank_card_list)
    RecyclerView thankCardList;

    private ThankCardItemAdapterHolder thankAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initBaseData();
        initBaseView();
        requestData();
    }

    public static void startActivity(Context mContext) {
        IntentUtils.startActivity(mContext, ThankCardPagerActivity.class);
    }

    private void initBaseData() {
        thankDataList = new ArrayList<>();
    }


    private void initBaseView() {
        initErrorLayout();
        thankAdapter = new ThankCardItemAdapterHolder(this, thankDataList);
        thankCardList.setHasFixedSize(true);
        thankCardList.setNestedScrollingEnabled(false);
        thankCardList.setLayoutManager(new LinearLayoutManager(this));
        thankCardList.setItemAnimator(new DefaultItemAnimator());
        thankCardList.setAdapter(thankAdapter);
    }

    private void initErrorLayout() {
        errorLayout.setVisibility(View.VISIBLE);
        errorLayout.removeAllViews();
        errorLayout.addView(ErrorViewCreateUtils.createLoadingView(this));
    }

    private void requestData() {
        UserManageDataApiStoreIml.getInstance().getThankCard().subscribe(new Observer<List<ThankDetailsData>>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                ApiException.showException(e, getActivityContext());
                errorLayout.setVisibility(View.VISIBLE);
                errorLayout.removeAllViews();
                errorLayout.addView(ErrorViewCreateUtils.createNoNetworkView(getActivityContext(), v -> {
                    initErrorLayout();
                    requestData();
                }));
            }

            @Override
            public void onNext(List<ThankDetailsData> thankDetailsData) {
                thankDataList.clear();
                thankDataList.addAll(thankDetailsData);
                if (thankDetailsData.size() > 0) {
                    errorLayout.setVisibility(View.INVISIBLE);
                    thankAdapter.notifyDataSetChanged();
                } else {
                    errorLayout.setVisibility(View.VISIBLE);
                    errorLayout.removeAllViews();
                    errorLayout.addView(ErrorViewCreateUtils.createNoMsgView(getActivityContext()));
                }
            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_thank_card_layout;
    }


    @OnClick({R.id.base_back_icon})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_back_icon:
                onBackPressed();
                break;
        }
    }
}
